
#include "file_list.h"


//*******************************************************************
int  Is_file_exist(const char* filename)
{

	FILE* fs = NULL;

	fs=fopen(filename,"r");
	if (!fs)
	{
		return 0;
	}

	fclose(fs);
	fs = NULL;

	return 1;
}

int  Is_file_jpg(const char* filename)
{
	int len = 0;

	if(!filename)
		return 0;

	len = strlen(filename);

	
    //printf("len = %d\n",len);
    //printf("file_3 = %c\n",filename[len - 3]);
    //printf("file_2 = %c\n",filename[len - 2]);
    //printf("file_1 = %c\n",filename[len - 1]);
	
	if((filename[len - 3] == 'j')&&(filename[len - 2] == 'p')&&(filename[len - 1] == 'g'))
	{
		return 1;
	}
	
	return 0;
}


//*******************************************************************
//mode 1:get file form txt
//*******************************************************************

static unsigned long total_picture_file = 0;

static char* picture_data_buff = NULL;

static char  picture_path[1024];

int get_filename_from_txt(void)
{
	int file_len = 0;

	FILE* fs = NULL;

	char filename[1024];

	fs=fopen("./filelist.txt","rb");
	if (!fs)
	{
		return 0;
	}

    fseek(fs, 0, SEEK_END);
    file_len = ftell(fs);
    fseek(fs, 0, SEEK_SET);

	total_picture_file = file_len/1024;
	
    printf("file_len = %d\n",file_len);
    printf("file_num = %d\n",total_picture_file);

    picture_data_buff = (char *)malloc(file_len);
    if(!picture_data_buff){
        printf("malloc error\n");
        fclose(fs);
        fs = NULL;
        return 0;
    }

	fread(picture_data_buff,file_len,1,fs);

	fclose(fs);
	fs = NULL;
	
    return 0;

}

void picture_buff_free(void)
{
	if(picture_data_buff){
		free(picture_data_buff);
		picture_data_buff = NULL;
	}
}


int search_picture_path(  const char *path)
{
	DIR *dir;
	struct dirent *ptr;

	if ((dir = opendir(path)) == NULL)
	{
		printf("Open dir error...");
		exit(1);
	}

	while ((ptr = readdir(dir)) != NULL)
	{
		if(strcmp(ptr->d_name, ".")==0 || strcmp(ptr->d_name, "..")==0)    ///current dir OR parrent dir
			continue;
	
		switch (ptr->d_type)
		{
			case DT_REG:	
			{

				break;
			}
			case DT_DIR:	
			{
				memset(picture_path,'\0',sizeof(picture_path));
				strcpy(picture_path,path);
				strcat(picture_path,"/");
				strcat(picture_path,ptr->d_name);

				printf("picture_path=%s\n",picture_path);
				
				break;
			}
			case DT_UNKNOWN:break;
			default:break;
		}
	}

	closedir(dir);
}


void get_file_full_path_by_buff(int index,char *filename)
{	

	char filetemp[1024];
	
	char * used_buff = NULL;

	if(!filename)
		return ;

	if(!picture_data_buff)
		return ;
	
	if(strlen(picture_path) <= 0)
		return ;

	
	if(index >= total_picture_file)
		index = 0; 
	
	used_buff = picture_data_buff + index*1024;


	memset(filetemp,0,sizeof(filetemp));
	
	memcpy(filetemp,used_buff,1024);
	
	strcpy(filename,picture_path);
	strcat(filename,"/");
	strcat(filename,filetemp);
	
	//log_i("filename = %s\n",filename);


}

void get_file_full_path_by_fd(int index,char *filename)
{	

	char filetemp[1024];
	FILE* fs = NULL;

	if(!filename)
		return ;

	if(strlen(picture_path) <= 0)
		return ;

	if(index >= total_picture_file)
		index = 0; 

	memset(filetemp,0,sizeof(filetemp));
	
	fs=fopen("./filelist.txt","rb");
	if (!fs)
	{
		return 0;
	}

    fseek(fs, index*1024, SEEK_SET);

	fread(filetemp,1024,1,fs);

	fclose(fs);
	fs = NULL;

	strcpy(filename,picture_path);
	strcat(filename,"/");
	strcat(filename,filetemp);
	
	//log_i("filename = %s\n",filename);

}


unsigned long file_list_get_num(void)
{
	return total_picture_file;
}

//*******************************************************************












